// nodejs代理服务器，解决跨域
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();

// 确保静态资源和node代理服务器是部署在同一个服务器上，二者之间不存在跨域
app.use(express.static("./public"));

const proxyMiddleware = createProxyMiddleware({
  target: 'http://localhost:8000',
  pathRewrite: {
    '^/api': '',
  },
  changeOrigin: true,
});
// 将/api开头的请求代理到8000端口上，node代理服务器和服务器间不存在跨域
app.use('/api', proxyMiddleware);

app.listen(8888, () => {
  console.log('8888端口服务开启！');
});
